﻿
#include <stdio.h>
#include <string.h>

const char* types[] = {"Decimal",
                       "Int64",
                       "Int32",
                       "Int16",
                       "SByte",
                       "UInt64",
                       "UInt32",
                       "UInt16",
                       "Byte",
                       "Single",
                       "Double",
                       "String"};
#define ARRAY_SIZE_0 "        Return If(i Is Nothing, 0, i.Length())\n"
#define ARRAY_LONG_SIZE_0 "        Return If(i Is Nothing, 0, i.LongLength())\n"
#define IS_EMPTY_ARRAY_0 "        Return i Is Nothing OrElse i.Length() = 0\n"
#define ARRAY_SIZE_1 "        Return If(i Is Nothing, 0, i.GetLength(0))\n"
#define ARRAY_LONG_SIZE_1 "        Return If(i Is Nothing, 0, i.GetLongLength(0))\n"
#define IS_EMPTY_ARRAY_1 "        Return i Is Nothing OrElse i.GetLength(0) = 0\n"

int main(int argc, char* argv[])
{
    if(argc < 2) printf("need a parameter for output file.\n");
    else
    {
        FILE* fo = fopen(argv[1], "w");
        fputs("\n'this file is generated by osi/root/codegen/array_size/array_size.exe"
              "\n'so edit the osi/root/codegen/array_size/array_size.cpp instead of this file\n"
              "\n\nPublic Module _array_size\n\n", fo);
        for(int i = 0; i < sizeof(types) / sizeof(types[0]); i++)
        {
            fprintf(fo,
                    "    Public Function array_size(ByVal i() As %s) As Int32\n"
                    ARRAY_SIZE_0
                    "    End Function\n\n",
                    types[i]);

            fprintf(fo,
                    "    Public Function array_long_size(ByVal i() As %s) As Int64\n"
                    ARRAY_LONG_SIZE_0
                    "    End Function\n\n",
                    types[i]);

            fprintf(fo,
                    "    Public Function isemptyarray(ByVal i() As %s) As Boolean\n"
                    IS_EMPTY_ARRAY_0
                    "    End Function\n\n",
                    types[i]);

            fprintf(fo,
                    "    Public Function array_size(ByVal i(,) As %s) As Int32\n"
                    ARRAY_SIZE_1
                    "    End Function\n\n",
                    types[i]);

            fprintf(fo,
                    "    Public Function array_long_size(ByVal i(,) As %s) As Int64\n"
                    ARRAY_LONG_SIZE_1
                    "    End Function\n\n",
                    types[i]);

            fprintf(fo,
                    "    Public Function isemptyarray(ByVal i(,) As %s) As Boolean\n"
                    IS_EMPTY_ARRAY_1
                    "    End Function\n\n",
                    types[i]);
        }

        fputs("    Public Function array_size(Of T)(ByVal i() As T) As Int32\n"
              ARRAY_SIZE_0
              "    End Function\n\n",
              fo);

        fputs("    Public Function array_long_size(Of T)(ByVal i() As T) As Int64\n"
              ARRAY_LONG_SIZE_0
              "    End Function\n\n",
              fo);

        fputs("    Public Function isemptyarray(Of T)(ByVal i() As T) As Boolean\n"
              IS_EMPTY_ARRAY_0
              "    End Function\n\n",
              fo);

        fputs("    Public Function array_size(Of T)(ByVal i(,) As T) As Int32\n"
              ARRAY_SIZE_1
              "    End Function\n\n",
              fo);

        fputs("    Public Function array_long_size(Of T)(ByVal i(,) As T) As Int64\n"
              ARRAY_LONG_SIZE_1
              "    End Function\n\n",
              fo);

        fputs("    Public Function isemptyarray(Of T)(ByVal i(,) As T) As Boolean\n"
              IS_EMPTY_ARRAY_1
              "    End Function\n\n",
              fo);

        fputs("End Module\n", fo);
        fclose(fo);
    }
}
